//- Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. 
//- <https://www.chatopera.com>, Licensed under the Chunsong Public 
//- License, Version 1.0  (the "License"), https://docs.cskefu.com/licenses/v1.html
//- Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
//- Copyright (C) 2018-Jun. 2023 Chatopera Inc, <https://www.chatopera.com>
//- Licensed under the Apache License, Version 2.0
//- http://www.apache.org/licenses/LICENSE-2.0
.uk-layui-form
    form.layui-form.uk-form(action='/admin/organ/save.html')
        input#area(type='hidden', name='area')
        .layui-form-item
            label.layui-form-label 部门：
            .layui-input-inline
                input.layui-input(type='text', name='name', required, lay-verify='required', placeholder='请输入部门名称', autocomplete='off')
        .layui-form-item
            .layui-inline
                label.layui-form-label 上级机构：
                .layui-input-inline(style='position: relative;')
                    input#parent(type='hidden', name='parent', value=(organ ? organ.id : 0))
                    input#parentname.layui-input(type='text', name='parentname', required, value=(organ ? organ.name : area ? area.name : '组织机构'), lay-verify='required', autocomplete='off', readonly, onclick='showMenu();')
                    i.layui-icon(style='position: absolute;right: 3px;top: 6px;font-size: 25px;(#{systemConfig.color ? systemConfig.color : "color:#0096C4"})', onclick='showMenu()') 
                    #menuContent.ukefu-ztree(style='display:none; position: absolute;z-index:10000;width: 218px;')
                        ul#organTree.ztree(style='width:208px;')
        .layui-form-item
            label.layui-form-label 启用技能组：
            .layui-input-block
                input(type='checkbox', name='skill', lay-skin='switch', lay-filter='area', value='1', lay-text='开启|关闭')
        .layui-form-button
            .layui-button-block
                button.layui-btn(lay-submit, lay-filter='formDemo') 立即提交
                button.layui-btn.layui-btn-original(type='reset') 重置
script.
    //Demo
    layui.use('form', function () {
        var form = layui.form();
        form.render(); //更新全部
        form.verify({
            required: function (value) {
                if (value.length > 50) {
                    return "部门名称不能超过50字";
                }
            }
        })
        form.on("switch(area)", function (data) {
            if (data.elem.checked) {
                $('#area').show();
            } else {
                $('#area').hide();
            }
        });
    });

script(type='text/javascript').
    var setting = {
        data: {simpleData: {enable: true}},
        callback: {onClick: onClick, beforeClick: beforeClick}
    };
    function beforeClick(treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("organTree")
        nodes = zTree.getSelectedNodes();
        var allow = true, lastnode;
        for (var i = 0, l = nodes.length; i < l; i++) {
            lastnode = nodes[i].type;
            if (nodes[i].organ && (nodes.length >= 1 && (event.ctrlKey || event.shiftKey))) {
                allow = false;
                break;
            }
        }
        if (lastnode != treeNode.type && (event.ctrlKey || event.shiftKey)) {
            allow = false;
        }
        if (allow == false) {
            top.layer.alert("组织机构不能点选多个上级机构，也不能同时选择地区和机构", {icon: 3});
            return false;
        } else {
            return true;
        }
    }
    function onClick(e, treeId, treeNode) {
        var zTree = $.fn.zTree.getZTreeObj("organTree"), nodes = zTree.getSelectedNodes(), v = "", value = "";
        nodes.sort(function compare(a, b) {
            return a.id - b.id;
        });
        for (var i = 0, l = nodes.length; i < l; i++) {
            v += nodes[i].name + ",";
            if (value != "") {
                value = value + ",";
            }
            value = nodes[i].id;
        }
        if (v.length > 0) v = v.substring(0, v.length - 1);
        if (treeNode.type == "area") {
            $("#parentname").attr("value", v);
            $("#parent").attr("value", "0");
            $("#area").attr("value", value);
        } else {
            $("#parentname").attr("value", v);
            $("#parent").attr("value", value);
            $("#area").attr("value", "");
        }
        if (!event.ctrlKey && !event.shiftKey) {
            hideMenu();
        }
    }
    var zNodes = !{pugHelper.toJSON(organList)}
    $.each(zNodes, function (i, o) {
        o.id = o.id;
        o.pId = o.parent ? o.parent : '0';
        o.name = o.name;
        o.open = true;
        o.organ = true;
        o.type = "organ";
        o.icon = '/images/dept.png'
    });
    console.log(zNodes)
    $(document).ready(function () {
        var ztree = $.fn.zTree.init($("#organTree"), setting, zNodes);
        if ('#{organ}') {
            var node = ztree.getNodeByParam('id', '#{organ.id}');//获取id为1的点
            ztree.selectNode(node);//选择点
        }
        ;
        if ('#{area}') {
            var node = ztree.getNodeByParam('id', '#{area.id}');//获取id为1的点
            ztree.selectNode(node);//选择点
        }
    });
    function showMenu() {
        $("#menuContent").css({left: "0px", top: "38px"}).show();
        $("body").bind("mousedown", onBodyDown);
    }
    function hideMenu() {
        $("#menuContent").hide();
        $("body").unbind("mousedown", onBodyDown);
    }
    function onBodyDown(event) {
        if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length > 0)) {
            hideMenu();
        }
    }
            
